package com.mocha.bss.domain.rbac.contoller;

import com.coffee.core.*;
import com.coffee.log.capture.OperationLog;
import com.mocha.bss.domain.org.contoller.query.UserQuery;
import com.mocha.bss.domain.rbac.repository.MochaRoleRepository;
import com.mocha.bss.domain.rbac.service.MochaRoleFunctionService;
import com.mocha.bss.domain.rbac.service.PermissionService;
import com.mocha.bss.feign.bo.UserInfoBO;
import com.mocha.bss.feign.bo.menu.PermissionVO;
import com.mocha.bss.org.vo.UserVO;
import com.mocha.bss.rbac.bo.RoleBO;
import com.mocha.bss.rbac.bo.RoleFunctionBO;
import com.mocha.bss.rbac.vo.CoordinateVO;
import com.mocha.bss.rbac.vo.RoleFunctionVO;
import com.pinghua.mocha.basics.dmo.RoleVO;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author songkui
 * @since 2022-02-11
 */
@Slf4j
@RestController
@RequestMapping("/rbac")
@AllArgsConstructor
public class PermissionController {

    private final PermissionService permissionService;

    private final MochaRoleRepository roleService;

    private final MochaRoleFunctionService mochaRoleFunctionService;

    /** 登录成功后 调用 该接口 */
    @GetMapping("/permission")
    public CoffeeResponse<PermissionVO> findPermission(HttpServletRequest request) {
        return CoffeeResponse.responseSuccess(permissionService.buildPermission(CoffeeHeader.getUserId(request)));
    }

    /** 点位 */
    @GetMapping("/positioning")
    public CoffeeResponse<CoordinateVO> positioning(HttpServletRequest request) {
        return CoffeeResponse.responseSuccess(permissionService.positioning(CoffeeHeader.getUserId(request)));
    }

    /**
     * 新增角色
     * @param
     * @return
     */
    @OperationLog(scope = "PC", name = "新增角色")
    @PostMapping("/save")
    public CoffeeResponse<?> saveRole(@Validated({ValidGroup.Create.class}) @RequestBody RoleBO roleBO) {
        roleService.saveRole(roleBO);
        return CoffeeResponse.response(CoffeeResponseEnum.Success);
    }

    /**
     * 更新角色
     * @param
     * @return
     */
    @OperationLog(scope = "PC", name = "更新角色")
    @PutMapping("/update")
    public CoffeeResponse<?> updateRole(@Validated(ValidGroup.Update.class) @RequestBody RoleBO roleBO) {
        roleService.updateRole(roleBO);
        return CoffeeResponse.response(CoffeeResponseEnum.Success);
    }

    /**
     * 删除角色
     * @param
     * @return
     */
    @OperationLog(scope = "PC", name = "删除角色")
    @DeleteMapping("/delete/{id}")
    public CoffeeResponse<?> deleteById(@PathVariable Integer id) {
        roleService.removeById(id);
        return CoffeeResponse.response(CoffeeResponseEnum.Success);
    }

    /**
     * 查找角色列表
     * @return
     */
    @PostMapping("/list")
    public CoffeeResponse<List<com.pinghua.mocha.basics.dmo.RoleVO>> list(@RequestBody RoleBO roleBO){
        List<RoleVO> list = roleService.listByQuery(roleBO);
        return CoffeeResponse.responseSuccess(list);
    }

    /**
     * 分页查询角色列表
     * @param query
     * @return
     */
    @OperationLog(scope = "PC", name = "分页查询角色列表")
    @PostMapping("/page")
    public CoffeeResponse<CoffeePage<UserVO>> page(@RequestBody UserQuery<UserInfoBO> query){
        return CoffeeResponse.responseSuccess(null);
    }





    /** 查询功能权限列表-roleId非必穿 */
    @GetMapping("/menu")
    public CoffeeResponse<RoleFunctionVO> menu(@RequestParam(value ="roleId", required = false) Long roleId) {
        return CoffeeResponse.responseSuccess(mochaRoleFunctionService.findMenuRole(roleId));
    }

    /** 添加用户-功能权限 */
    @OperationLog(scope = "PC", name = "添加用户-功能权限")
    @PostMapping("/add-menu")
    public CoffeeResponse<?> addRoleMenu(@Validated(ValidGroup.Update.class) @RequestBody RoleFunctionBO roleFunctionBO){
        mochaRoleFunctionService.addMenuRole(roleFunctionBO);
        return CoffeeResponse.success();
    }







}
